package com.hivekion.common.Date;

import cn.hutool.core.date.DateUtil;
import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.*;

public class DateUtils {
    private static final int Hour_Second = 60 * 60;
    private static final int Minute_Second = 60;

    public static int getNowYear() {
        SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy");
        Date date = new Date();
        String year = yearFormat.format(date);
        return Integer.valueOf(year);
    }

    public static String getNowDay() {
        SimpleDateFormat dayFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        return dayFormat.format(date);
    }

    public static String getNowMillisecond() {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        return simpleDateFormat.format(date);
    }

    public static String getRandomFileName() {
        Date date = new Date(System.currentTimeMillis());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        return simpleDateFormat.format(date);
    }

    public static Date getPastDate(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - past);
        Date date = calendar.getTime();
        return date;
    }

    /**
     * 字符串转date
     * @param dates
     * @return
     */
    public static Date convertStringToDate(String dates){
        Date date = new Date();
        if (StringUtils.isNoneBlank(dates)){
            try {
                date = new SimpleDateFormat("yyyy-MM-dd").parse(dates);
            }catch (ParseException ex){

            }
        }
        return date;
    }

    /**
     * 时间相差天数
     * @param start
     * @param end
     * @return
     */
    public static int getTimeIntervalDay(Date start,Date end){
        int days = (int) ((start.getTime() - end.getTime()) / (1000*3600*24));
        return days;
    }

    /**
     * 时间相差天数
     * @param start
     * @param end
     * @return
     */
    public static List<String> getTimeIntervalDayList(Date start,Date end){
        List<String> dateList = new ArrayList<>();
        SimpleDateFormat outformat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar sCalendar = Calendar.getInstance();
        sCalendar.setTime(start);
        int year = sCalendar.get(Calendar.YEAR);
        int month = sCalendar.get(Calendar.MONTH);
        int day = sCalendar.get(Calendar.DATE);
        sCalendar.set(year, month, day, 0, 0, 0);
        Calendar eCalendar = Calendar.getInstance();
        eCalendar.setTime(end);
        year = eCalendar.get(Calendar.YEAR);
        month = eCalendar.get(Calendar.MONTH);
        day = eCalendar.get(Calendar.DATE);
        eCalendar.set(year, month, day, 0, 0, 0);
        while (sCalendar.before(eCalendar))
        {
            dateList.add(outformat.format(sCalendar.getTime()));
            sCalendar.add(Calendar.DAY_OF_YEAR, 1);
        }
        dateList.add(outformat.format(eCalendar.getTime()));
        return dateList;
    }

    /**
     * 获取2个日期的周间隔
     * @param startDate
     * @param endDate
     * @return
     */
    public static List<String> getTimeIntervalWeekList(String startDate,String endDate){
        List<String> dateList = new ArrayList<>();
        Integer startYear = Integer.parseInt(startDate.substring(0,startDate.lastIndexOf("-")));
        Integer startWeek = Integer.parseInt(startDate.substring(startDate.lastIndexOf("-")+1,startDate.lastIndexOf("周")));
        Integer endYear = Integer.parseInt(endDate.substring(0,endDate.lastIndexOf("-")));
        Integer endWeek = Integer.parseInt(endDate.substring(endDate.lastIndexOf("-")+1,endDate.lastIndexOf("周")));
        if (startYear.equals(endYear)){
            for (int i = startWeek; i <= endWeek;i++){
                String date = startYear + "-" + i + "周";
                dateList.add(date);
            }
        }else {
            for (int i = startYear; i <= endYear;i++) {
                if (startYear.equals(i)){
                    for (int j = startWeek; j <= 52; j++) {
                        String date = i + "-" + j + "周";
                        dateList.add(date);
                    }
                }else if (endYear.equals(i)){
                    for (int j = 1; j <= endWeek; j++) {
                        String date = i + "-" + j + "周";
                        dateList.add(date);
                    }
                }else {
                    for (int j = 1; j <= 52; j++) {
                        String date = i + "-" + j + "周";
                        dateList.add(date);
                    }
                }
            }
        }
        return dateList;
    }

    public static int getDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.DATE);
    }

    /** * 返回日期的月份，1-12,即yyyy-MM-dd中的MM * * @param date * @return */
    public static int getMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MONTH) + 1;
    }

    /** * 返回日期的年,即yyyy-MM-dd中的yyyy * * @param date * Date * @return int */
    public static int getYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.YEAR);
    }

    public static int getDaysOfMonth(int year, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month - 1, 1);
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }


    public static int calDiffMonth(String startDate,String endDate){
        int result=0;
        try {
            SimpleDateFormat sfd=new SimpleDateFormat("yyyy-MM");
            Date start = sfd.parse(startDate);
            Date end = sfd.parse(endDate);
            int startYear=getYear(start);
            int startMonth=getMonth(start);
            int startDay=getDay(start);
            int endYear=getYear(end);
            int endMonth=getMonth(end);
            int endDay=getDay(end);
            if (startDay>endDay){ //1月17 大于 2月28
                if (endDay==getDaysOfMonth(getYear(new Date()),2)){   //也满足一月
                    result=(endYear-startYear)*12+endMonth-startMonth;
                }else{
                    result=(endYear-startYear)*12+endMonth-startMonth-1;
                }
            }else{
                result=(endYear-startYear)*12+endMonth-startMonth;
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }

        return result;
    }

    /**
     * 获取2个日期的月份
     * @param startDate
     * @param endDate
     * @return
     */
    public static List<String> getTimeIntervalMonthList(String startDate,String endDate){
        List<String> dateList = new ArrayList<>();
        // 判断两个月份的间隔数量
        int count = calDiffMonth(startDate,endDate);
        // 如果两个日期相等
        if(count == 0) {
            dateList.add(startDate);
            dateList.add(endDate);
            return dateList;
        }
        if(count > 0) {
            YearMonth now = YearMonth.parse(startDate, DateTimeFormatter.ofPattern("yyyy-MM"));
            dateList.add(startDate);
            for (int i = 1; i <= count; i++) {
                // 参数-1 是获取下个月的月份
                YearMonth nextMonths = now.minusMonths(-i);
                dateList.add(nextMonths.toString());
            }
            return dateList;
        }
        if(count < 0) {
            YearMonth now = YearMonth.parse(startDate, DateTimeFormatter.ofPattern("yyyy-MM"));
            dateList.add(startDate);
            count = Math.abs(count);
            for (int i = 1; i <= count; i++) {
                // 参数1 是获取上个月的月份
                YearMonth nextMonths = now.minusMonths(i);
                dateList.add(nextMonths.toString());
            }
            Collections.reverse(dateList);
            return dateList;
        }
        return dateList;
    }

    /**
     * 获取2个日期的月份
     * @param startDate
     * @param endDate
     * @return
     */
    public static List<String> getTimeIntervalYearList(String startDate,String endDate){
        List<String> dateList = new ArrayList<>();

        Integer start = Integer.parseInt(startDate);
        Integer end = Integer.parseInt(endDate);
        for (int i = start; i <= end;i++){
            dateList.add(Integer.toString(i));
        }
        return dateList;
    }

    /**
     * 某周的第一天
     *
     * @param year
     * @param week
     * @return
     */
    public static Date getFirstDayOfWeek(int year, int week) {
        Calendar cal = Calendar.getInstance();
        // 设置年份
        cal.set(Calendar.YEAR, year);
        // 设置周
        cal.set(Calendar.WEEK_OF_YEAR, week);
        // 设置该周第一天为星期一
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);

        return cal.getTime();
    }

    /**
     * 某周的最后一天
     *
     * @param year
     * @param week
     * @return
     */
    public static Date getLastDayOfWeek(int year, int week) {
        Calendar cal = Calendar.getInstance();
        // 设置年份
        cal.set(Calendar.YEAR, year);
        // 设置周
        cal.set(Calendar.WEEK_OF_YEAR, week);
        // 设置该周第一天为星期一
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        // 设置最后一天是星期日
        cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek() + 5); // Sunday
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);

        return cal.getTime();
    }

    /**
     * 某月份的第一天
     * @param year
     * @param month
     * @return
     */
    public static Date getFirstDayOfMonth(int year,int month)
    {
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR,year);
        //设置月份
        cal.set(Calendar.MONTH, month-1);
        //获取某月最小天数
        int firstDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
        //设置日历中月份的最小天数
        cal.set(Calendar.DAY_OF_MONTH, firstDay);
        return cal.getTime();
    }

    /**
     * 某月份的最后一天
     * @param year
     * @param month
     * @return
     */
    public static Date getLastDayOfMonth(int year,int month)
    {
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR,year);
        //设置月份
        cal.set(Calendar.MONTH, month-1);
        //获取某月最大天数
        int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        //设置日历中月份的最大天数
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        return cal.getTime();
    }

    /**
     * 某年份的第一天
     *
     * @param year 年份
     */
    public static Date getFirstDateForYear(int year) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, calendar.getActualMinimum(Calendar.MONTH));
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        return calendar.getTime();
    }

    /**
     * 某年份的最后一天
     *
     * @param year 年份
     */
    public static Date getLastDateForYear(int year) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, calendar.getActualMaximum(Calendar.MONTH));
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        return calendar.getTime();
    }

    /**
     * 秒数转换00:00:00
     *
     * @param second
     * @return
     */
    public static String getTimeStrBySecond(Integer second) {
        if (second <= 0) {
            return "00:00:00";
        }
        int hour = second / Hour_Second;
        if (hour > 0) {
            second -= hour * Hour_Second;
        }
        int minute = second / Minute_Second;
        if (minute > 0) {
            second -= minute * Minute_Second;
        }
        return (hour >= 10 ? (hour + "")
                : ("0" + hour) + ":" + (minute >= 10 ? (minute + "") : ("0" + minute)) + ":"
                + (second >= 10 ? (second + "") : ("0" + second)));
    }
}
